package com.nangle.managehost.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.nangle.managehost.R;
import com.nangle.managehost.entity.BedMessage;
import com.nangle.managehost.entity.OperationMsg;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by YFB on 2017/4/12.
 */

public class ClientAdapter extends BaseAdapter{
    private Context context;
    private LayoutInflater mInflater;
    private List<BedMessage> bedMessages = new ArrayList<>();
    private boolean isDoctor = false;
    public ClientAdapter(Context context,boolean isDoctor){
        this.context = context;
        mInflater = LayoutInflater.from(context);
        this.isDoctor = isDoctor;
    }
    public void setData(List<BedMessage> bedMessages){
        this.bedMessages = bedMessages;
    }
    @Override
    public int getCount() {
        return bedMessages.size();
    }

    @Override
    public Object getItem(int position) {
        return bedMessages.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.layout_client, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.tvName = (TextView)convertView.findViewById(R.id.c_name);
            viewHolder.tvBed = (TextView)convertView.findViewById(R.id.c_bed);
            viewHolder.tvContent = (TextView)convertView.findViewById(R.id.tv_content);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.tvBed.setText(bedMessages.get(position).getBedNumber()+"床");
        viewHolder.tvName.setText(bedMessages.get(position).getPatientName());
        List<OperationMsg> operationMsgs = bedMessages.get(position).getOperationMsgs();
        int count = operationMsgs.size();
        StringBuffer stringBuffer = new StringBuffer();
        for(int i = 0;i<count;i++){
            if(isDoctor)
                stringBuffer.append(operationMsgs.get(i).getOperationName());
            else {
                stringBuffer.append(operationMsgs.get(i).getTime());
                stringBuffer.append("\n");
                stringBuffer.append(operationMsgs.get(i).getOperationName());
            }
            if(i<count-1){
                stringBuffer.append("\n");
            }
        }
        viewHolder.tvContent.setText(stringBuffer.toString());

        return convertView;
    }
    private class ViewHolder {
        private TextView tvName;
        private TextView tvBed;
        private TextView tvContent;
    }
}
